package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Role;
import com.hzdl.car.service.RoleService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.util.ResultObj;
import com.hzdl.car.vo.RoleVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.PublicKey;

@RestController
@RequestMapping("role")
public class RoleController {
 @Resource
    private RoleService roleService;
 @RequestMapping("findRole")
    public HzdlResultJson findRole(RoleVo roleVo){
        PageInfo<Role> pageInfo=roleService.findRole(roleVo);
        return HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("addRole")
    public ResultObj addRole(RoleVo roleVo){
     try {
         roleService.addRole(roleVo);
         return ResultObj.ADD_SUCCESS;
     }catch(Exception e) {
        return ResultObj.ADD_ERROR;
    }
    }
    @RequestMapping("updateRole")
    public ResultObj updateRole(RoleVo roleVo){
     try {
         roleService.update(roleVo);
         return ResultObj.UPDATE_SUCCESS;
     }catch (Exception e){
         return ResultObj.UPDATE_ERROR;
     }
    }
    @RequestMapping("delRole")
    public ResultObj delRole(RoleVo roleVo){
      try {
          roleService.delRole(roleVo);
          return ResultObj.DELETE_SUCCESS;
      }catch (Exception e){
          return ResultObj.DELETE_ERROR;
      }
    }
    @RequestMapping("chkRoleUserMenu")
    public HzdlResultJson chkRoleUserMenu(RoleVo roleVo){
     try {
         int row=roleService.chkRoleUserMenu(roleVo);
         return  HzdlResultJson.error(row,"");
     }catch (Exception e){
         return HzdlResultJson.error(-1,"");
     }
    }
    @RequestMapping("roleDelChk")
    public ResultObj roleDelChk(@RequestParam(value = "roleIds[]") Integer [] roleIds){
     try {
         roleService.roleDelChk(roleIds);
         return ResultObj.DELETE_SUCCESS;
     }catch (Exception e){
         return ResultObj.DELETE_ERROR;
     }
    }
    @RequestMapping("roleTreeJson")
    public HzdlResultJson roleTreeJson(RoleVo roleVo){
    try {
        return HzdlResultJson.ok(roleService.findMenuByRole(roleVo));
    }catch (Exception e){
     return HzdlResultJson.error(-1,e.getMessage());
    }
    }
    @RequestMapping("saveRoleMenu")
    public ResultObj saveRoleMenu(RoleVo roleVo){
     try {
         roleService.saveRoleMenu(roleVo);
         return ResultObj.DISPATCH_SUCCESS;
     }catch (Exception e){
         return  ResultObj.DISPATCH_ERROR;
     }
    }

}
